package com.lsw.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.lsw.R;
import com.lsw.util.InputTools;


/***
 * 从底部弹出的视图
 *
 * @author heiyue
 * @Desc
 * @Email heiyue623@126.com
 * @Date 2015-9-24
 */
public class ActionSheetDialogUtil {
    private Dialog dialog;
    private LinearLayout lLayout_content;
    private Display display;

    /**
     * @param context
     * @param contentView 弹出视图的内容区
     */
    public ActionSheetDialogUtil(Context context, View contentView) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();

        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.view_actionsheet_util, null);

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 获取自定义Dialog布局中的控件
        lLayout_content = (LinearLayout) view.findViewById(R.id.lLayout_content);
        lLayout_content.addView(contentView);
        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
    }

    public ActionSheetDialogUtil setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public ActionSheetDialogUtil setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    /**
     * 显示弹出框
     */
    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    /**
     * 隐藏对话框
     */
    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            InputTools.HideKeyboard(lLayout_content);
        }
    }

}
